package com.inossem.warehousestorage_inossem.adapter.changeStorage.chuangyou;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.change_storage.chuangyou.CYChangeStorageMaterielBatchDetailActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.chuangyou.CYChangeStorageMaterielListActivity;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageMaterialBatchBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageMaterialListBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.BatchBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.view.ExtendedEditText;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/30 9:52
 * @email siyu.wang@inossem.com
 */
public class CYChangeStorageMaterialDetailtAdapter extends BaseAdapter{
    private Context mContext;
    private List<BatchBean.BatchInfoBean> list = new ArrayList<>();
    private ItemListBean bean;
    private LayoutInflater inflater;
    private String mCondition;
    private EditText codeEditText;
    private CYChangeStorageMaterielBatchDetailActivity.ChangeStorageMaterielListener listener;
    private String mMaterialCode , mMaterialDescribe , mLocation , mOrderNumber , mChangeStorageNumber , mSupplier , unit;
    private float count;

    public CYChangeStorageMaterialDetailtAdapter(Context context , String condition ,String location , ItemListBean bean , CYChangeStorageMaterielBatchDetailActivity.ChangeStorageMaterielListener listener) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        this.listener = listener;
        this.mCondition = condition;
        this.bean = bean;
        this.list = bean.getBatchList() == null ? new ArrayList<>() : bean.getBatchList();
        mMaterialCode = bean.getMat_code();
        mMaterialDescribe = bean.getMat_name();
        mLocation = location;
        mOrderNumber = bean.getOrder_qty();
        mSupplier = bean.getSupplier_name();
        unit = bean.getUnit_name();
        mChangeStorageNumber = TextUtils.isEmpty(bean.getActualSendNumber()) ? "0" : bean.getActualSendNumber();
        count = Float.parseFloat(mChangeStorageNumber);

    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.cy_view_change_storage_material_detail_first_item, null);
            codeEditText = (EditText) convertView.findViewById(R.id.code);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            codeEditText.setHint("请输入批次");
            TextView materialCode = (TextView) convertView.findViewById(R.id.materialCode);
            TextView materialDescribe = (TextView) convertView.findViewById(R.id.materialDescribe);
            TextView location = (TextView) convertView.findViewById(R.id.location);
            TextView orderNumber = (TextView) convertView.findViewById(R.id.orderNumber);
            TextView changeStorageNumber = (TextView) convertView.findViewById(R.id.changeStorageNumber);
            TextView supplier = (TextView) convertView.findViewById(R.id.supplier);
            TextView orderNumberUnit = (TextView) convertView.findViewById(R.id.orderNumberUnit);
            orderNumberUnit.setText(unit);
            TextView changeStorageNumberUnit = (TextView) convertView.findViewById(R.id.changeStorageNumberUnit);
            changeStorageNumberUnit.setText(unit);
            materialCode.setText(mMaterialCode);
            materialDescribe.setText(mMaterialDescribe);
            location.setText(mLocation);
            orderNumber.setText(mOrderNumber);
            changeStorageNumber.setText(mChangeStorageNumber);
            supplier.setText(mSupplier);
            if(!TextUtils.isEmpty(mCondition)){
                codeEditText.setText(mCondition);
            }
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    listener.onNoDoubleClickListener(TextUtils.isEmpty(codeEditText.getText().toString()) ? "" : codeEditText.getText().toString());

                }
            });
        } else {
            BatchBean.BatchInfoBean materielItem = list.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.cy_view_change_storage_materiel_detail, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, materielItem , position);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.position = (TextView) view.findViewById(R.id.position);
        viewHolder.batch = (TextView) view.findViewById(R.id.batch);
        viewHolder.supplier = (TextView) view.findViewById(R.id.supplier);
        viewHolder.inventoryQuantity = (TextView) view.findViewById(R.id.inventoryQuantity);
        viewHolder.inventoryQuantityUnit = (TextView) view.findViewById(R.id.inventoryQuantityUnit);
        viewHolder.changeStorageNumberUnit = (TextView) view.findViewById(R.id.changeStorageNumberUnit);
        viewHolder.changeStorageNumber = (ExtendedEditText) view.findViewById(R.id.changeStorageNumber);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, BatchBean.BatchInfoBean item , int position) {
        viewHolder.position.setText(item.getPosition_code());
        viewHolder.batch.setText(item.getBatch());
        viewHolder.supplier.setText(item.getSupplier_batch());
        viewHolder.inventoryQuantity.setText(item.getStock_qty());
        viewHolder.inventoryQuantityUnit.setText(item.getUnit_name());
        viewHolder.changeStorageNumberUnit.setText(item.getUnit_name());

        viewHolder.changeStorageNumber.setTag(item);
        viewHolder.changeStorageNumber.clearFocus();
        viewHolder.changeStorageNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    viewHolder.changeStorageNumber.clearTextChangedListeners();

                    viewHolder.changeStorageNumber.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            CYChangeStorageMaterialBatchBean.BatchBean bean = (CYChangeStorageMaterialBatchBean.BatchBean) viewHolder.changeStorageNumber.getTag();
                            bean.setOutStorageNumber(charSequence + "");
                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                            String number =  TextUtils.isEmpty(viewHolder.changeStorageNumber.getText().toString()) ? "0" : viewHolder.changeStorageNumber.getText().toString();
                            if ((count + Float.parseFloat(number)) > Float.parseFloat(mOrderNumber)) {
                                Utils.middleToast(mContext , "转储数量不能大于订单数量");
                            }else {
                                count += Float.parseFloat(viewHolder.changeStorageNumber.getText().toString());
                            }
                        }
                    });
                }else {

                }
            }
        });
        viewHolder.changeStorageNumber.setText(TextUtils.isEmpty(item.getQty()) ? "0" : item.getQty());

    }



    private class ViewHolder {
        private TextView position , batch, supplier , inventoryQuantity , inventoryQuantityUnit , changeStorageNumberUnit;
        private ExtendedEditText changeStorageNumber;
    }
}
